home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / LIFER__ / PROTO / U / COMMON_L.C < prev    next >
Text File  |  1991-08-02  |  5KB  |  208 lines

  1. /*  Common_Life */                                                        /* Common */
  2.  
  3. /* Unit name:  Common_Life.c   */
  4. /* Function:  Common variables for program specific code. */
  5. /* History: 7/23/91 Original by Prototyper 3.0   */
  6.  
  7. #include "PCommonLife.h"    /* Common */
  8. #include "Common_Life.h"    /* Common */
  9.  
  10.  
  11. struct table
  12.         {
  13.          char lines[11];/* USE VALUES 1 TO 10 */
  14.         };
  15.         
  16. static struct table new_display_table[6],display_table[6] =        /* USE VALUES 1 TO 5 */
  17.         {{' ','-','-','-','-','-','-','-','-','-','-'},/* SKIP */
  18.         {' ','-','-','-','-','-','-','-','-','-','-'},/* 1 */
  19.         {' ','-','-','-','-','-','-','-','-','-','-'},
  20.         {' ','-','-','-','-','-','-','-','-','-','-'},
  21.         {' ','-','-','-','-','-','-','-','-','-','-'},
  22.         {' ','-','-','-','-','-','-','-','-','-','-'}};/* 5 */
  23. struct table *pdisplay_table = display_table;
  24. struct table *pnew_display_table = new_display_table;
  25. /* display_table is used for the grid's output. */
  26. /* - = dead * = alive */
  27.  
  28. StringHandle    gDefaultTEX, gDefaultTEY;
  29. long             gX_Value, gY_Value, gCellCounter = 1;
  30.  
  31. /* ======================================================= */
  32.  
  33. Boolean CHECKXY(GetSelection, DType, DItem, tempRect)
  34. DialogPtr GetSelection;
  35. short DType;
  36. Handle DItem;
  37. Rect tempRect;
  38. {
  39.     Str255 sTemp;
  40.  
  41.     /* CHECK X FIRST */
  42.     GetDItem(GetSelection, InTEBoxX, &DType, &DItem, &tempRect);/* Get item information */
  43.         GetIText(DItem, &sTemp);
  44.         StringToNum(sTemp, &gX_Value);
  45.         
  46.     /* CHECK Y */
  47.     GetDItem(GetSelection, InTEBoxY, &DType, &DItem, &tempRect);/* Get item information */
  48.         GetIText(DItem, &sTemp);
  49.         StringToNum(sTemp, &gY_Value);        
  50.         if ((gX_Value < 1 || gX_Value > MaxColumns) || (gY_Value < 1 || gY_Value > 5))
  51.         {
  52.             PA_Life_Alert(); /* SHOW ALERT WINDOW */
  53.             return(0);
  54.         }
  55.         return(1); /* 1 = VALUES FOR X & Y ARE GOOD */
  56. }
  57.  
  58.  
  59.  
  60. /* ======================================================= */
  61. void STORE_VALUES(GetSelection, DType, DItem, tempRect)
  62. DialogPtr GetSelection;
  63. short DType;
  64. Handle DItem;
  65. Rect tempRect;
  66. {
  67.     /* STORE THE ARRAY xxx*/
  68.         display_table[gY_Value].lines[gX_Value] = '*';
  69.                 
  70.     /* CHECK TO SEE IF IT'S MORE THAN 10 CELLS */
  71.     /* IF NOT INCREASE AND DISPLAY COUNTER */
  72.     if (gCellCounter < MaxColumns)
  73.     {
  74.         /* INCREASE THE CELL COUNTER */
  75.         gCellCounter++; 
  76.             
  77.         /* UPDATE CELL COUNTER IN DISPLAY */
  78.         NumToString(gCellCounter, sTemp);
  79.         GetDItem(GetSelection,CellBox, &DType, &DItem, &tempRect);
  80.         SetIText(DItem,sTemp);
  81.     }
  82. }
  83.         
  84.         
  85. /* ======================================================= */
  86. /* DISPLAYS & COMPUTES GENERATIONS */
  87. void LIFE_PROGRAM()
  88.  
  89. {
  90.     DRAWNSTRING();
  91.     COMPUTE();
  92. }
  93.  
  94.  
  95.  
  96. /* ======================================================= */
  97. void COMPUTE(void)
  98.  
  99. {    
  100.     int counterY,counterX;
  101.     for(counterY=1;counterY <= MaxRows;counterY++)
  102.     {
  103.         for(counterX=1;counterX <= MaxColumns;counterX++)
  104.         {
  105.             switch(NEIGHBORCOUNT(counterY,counterX))
  106.             {
  107.                 case 0:
  108.                 case 1:
  109.                     (pnew_display_table + counterY)->lines[counterX] = '-';
  110.                     break;
  111.                 
  112.                 case 2:
  113.                     (pnew_display_table + counterY)->lines[counterX] = (pdisplay_table + counterY)->lines[counterX];
  114.                     break;
  115.                 
  116.                 case 3:
  117.                     (pnew_display_table + counterY)->lines[counterX] = '*';
  118.                     break;
  119.                     
  120.                 case 4:
  121.                 case 5:
  122.                 case 6:
  123.                 case 7:
  124.                 case 8:
  125.                     (pnew_display_table + counterY)->lines[counterX] = '-';
  126.                     break;
  127.             }/* switch */
  128.         }/* for counterX */
  129.     }/* for counterY*/
  130.     CopyMap();
  131. }
  132.  
  133. /* ======================================================= */
  134. int NEIGHBORCOUNT(int countY, int countX)
  135. {
  136.     int i,j;
  137.     int rlow,rhigh;
  138.     int clow, chigh;
  139.     int count = 0;
  140.     
  141.     if (countY <= 0)
  142.         rlow = 0;
  143.     else 
  144.         rlow = countY - 1;
  145.         
  146.     if (countY >= MaxRows - 1)
  147.         rhigh = MaxRows - 1;
  148.     else
  149.         rhigh= countY + 1;
  150.         
  151.     if (countX <= 0)
  152.         clow = 0;
  153.     else 
  154.         clow = countX - 1;
  155.         
  156.     if (countX >= MaxColumns - 1)
  157.         chigh = MaxColumns - 1;
  158.     else
  159.         chigh= countX + 1;
  160.  
  161.     for (i = rlow; i <= rhigh; i++)
  162.         for (j = clow; j <= chigh; j++)
  163.             if ((pdisplay_table + i)->lines[j] == '*')
  164.                 count++;
  165.     
  166.     if ((pdisplay_table + countY)->lines[countX] == '*')
  167.                 count--;
  168.                 
  169.     return count;
  170. }
  171.  
  172. /* ======================================================= */
  173. CopyMap()
  174.  
  175. {
  176.     int counterY,counterX;
  177.     for(counterY=1;counterY <= MaxRows;counterY++)
  178.     {
  179.         for(counterX=1;counterX <= 11;counterX++)
  180.         {
  181.             (pdisplay_table + counterY)->lines[counterX] = (pnew_display_table + counterY)->lines[counterX];
  182.         }
  183.     }
  184.  
  185. }
  186.  
  187.  
  188. /* ======================================================= */
  189. /* DRAWS ARRAY TO SCREEN */
  190. void DRAWNSTRING(void)
  191.  
  192. {
  193.     WindowPtr theWindow;
  194.     int counterY,counterX,MoveX=50, MoveY=25 ;
  195.     theWindow = FrontWindow();
  196.     EraseRect(&(theWindow->portRect));
  197.     Update_LIFE_WINDOW(theWindow);/* CHEATING HERE BY UPDATING THE WINDOW TO HAVE CONTROLS REDRAWN */
  198.     MoveTo(MoveX,MoveY);
  199.     for(counterY=1;counterY <= MaxRows;counterY++)
  200.     {
  201.         for(counterX=1;counterX <= MaxColumns;counterX++)
  202.         {
  203.             DrawChar((pdisplay_table + counterY)->lines[counterX]);
  204.         }
  205.         MoveY +=15;
  206.         MoveTo(MoveX,MoveY);
  207.     }
  208. }